package com.chezhujie.www.controller;

import com.chezhujie.www.domain.Czjdataresource;
import com.chezhujie.www.service.SortingService;
import com.xy.core.page.Pagination;
import com.xy.user.base.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 汤伟杰 on 2017/5/18.
 */
@Controller
@RequestMapping("/sorting")
public class SortingController extends BaseController<Czjdataresource> {
    @Autowired
    private SortingService sortingService;

    /**
     *
     * @param req
     * @param resp
     * @param model
     * @return
     */
    @RequestMapping({"/list.do",""})
    public String list(
            HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        model.addAttribute("msg", req.getParameter("msg"));
        Pagination pagination = sortingService.list();
        model.addAttribute("pagination", pagination);
        return "sorting/list";
    }

    /**
     * 排序位置交换
     * @param req
     * @param resp
     * @param model
     */
    @RequestMapping("/exchange.do")
    public String exchange(@RequestParam(required = true) Integer sorting,
                         @RequestParam(required = true) String id,
            HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model) throws IOException {
        Map<String, Object> map = new HashMap<String, Object>();
        String msg = "";
        try {
            if(sorting==1||sorting==-1){
                sortingService.exchange(id,sorting);
                map.put("status", "1");
            }else{
                throw new Exception("参数sorting只能传-1或1");
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status", "0");
            msg =""+ e.getMessage();
//            map.put("msg", e.getMessage());
        }
        return "redirect:list.do?msg="+msg;
//        resp.getWriter().print(JSONObject.toJSONString(map));

    }
    
  /**
   * 
   * 方法名：hyjg 显示会员价格
   * 创建人：RUI
   * 时间：2017年6月8日-下午2:12:26 
   * @param req
   * @param resp
   * @param model
   * @return
   * @throws Exception String
   * @exception 
   * @since  1.0.0
   */
    @RequestMapping("/hyjg")
    public String hyjg(HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model) throws Exception {
    	
        try {
			Pagination pagination = sortingService.listHyjg();
			model.addAttribute("pagination", pagination);
		} catch (Exception e) {
			e.printStackTrace();
		}
        
        
    	return "sorting/hyjg";

    }
    
    
    
    /**
     * 
     * 方法名：goUpdateHyjg 跳转都会员管理页面
     * 创建人：RUI
     * 时间：2017年6月8日-下午7:07:50 
     * @param dataresourceId
     * @param req
     * @param resp
     * @param model
     * @return
     * @throws Exception String
     * @exception 
     * @since  1.0.0
     */
    @RequestMapping("/goUpdateHyjg")
    public String goUpdateHyjg(String dataresourceId,HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model) throws Exception {
    	
    	try {
			Czjdataresource czjdataresource=sortingService.getCzjdataresourceByCode();
			model.addAttribute("czjdataresource", czjdataresource);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
      
    	return "sorting/goUpdateHyjg";
    }
    
	/**
     * 
     * 
     * 方法名：updateHyjg  修改会员
     * 创建人：RUI
     * 时间：2017年6月8日-下午7:08:31 
     * @param dataresourceId
     * @param value
     * @param req
     * @param resp
     * @param model
     * @return
     * @throws Exception String
     * @exception 
     * @since  1.0.0
     */
    @RequestMapping("/doUpdateHyjg")
    public String updateHyjg(String dataresourceId,java.math.BigDecimal value,HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model) throws Exception {
    	
    	try {
			sortingService.updateHyjg(value,dataresourceId);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return "redirect:/sorting/hyjg";
    }
    
    
    
}
